{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4936d1e6-5e7d-4e22-ae35-8e888927ce2d",
   "metadata": {},
   "source": [
    "# Use Pre-trained CNN as feature extractor"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf9e9fb5-7383-475a-93e1-decdbd59c247",
   "metadata": {},
   "source": [
    "Use MobileNetv3 as a feature extractor via the [embetter](https://github.com/koaning/embetter) scikit-learn library and [timm](https://github.com/rwightman/pytorch-image-models). Train a logistic regression classifier in scikit-learn on the embeddings."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96b717c7-54c9-40dc-ba80-0fb47da2c0bd",
   "metadata": {},
   "source": [
    "![](images/feature-extractor.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "64d1dd64-c45b-4092-84d1-1bfcd0998f15",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "# pip install gitpython\n",
    "from git import Repo\n",
    "\n",
    "if not os.path.exists(\"mnist-pngs\"):\n",
    "    Repo.clone_from(\"https://github.com/rasbt/mnist-pngs\", \"mnist-pngs\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3a892538-8d9b-4420-9525-26d1a4b37ae3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "\n",
    "for name in (\"train\", \"test\"):\n",
    "\n",
    "    df = pd.read_csv(f\"mnist-pngs/{name}.csv\")\n",
    "    df[\"filepath\"] = df[\"filepath\"].apply(lambda x: \"mnist-pngs/\" + x)\n",
    "    df = df.sample(frac=1, random_state=123).reset_index(drop=True)\n",
    "    df.to_csv(f\"mnist-pngs/{name}_shuffled.csv\", index=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5885e9bb-d43f-46ca-83ae-e2d63edcbb37",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1fba0fcb2b1f408f85013da0d1694dd3",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/60 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from sklearn.pipeline import make_pipeline\n",
    "from sklearn.linear_model import SGDClassifier\n",
    "from tqdm.notebook import tqdm\n",
    "\n",
    "# pip install \"embetter[vision]\"\n",
    "from embetter.vision import ImageLoader, TimmEncoder\n",
    "\n",
    "\n",
    "embed = make_pipeline(\n",
    "    ImageLoader(),\n",
    "    TimmEncoder(name=\"mobilenetv3_large_100\")\n",
    ")\n",
    "\n",
    "model = SGDClassifier(loss='log_loss', n_jobs=-1, shuffle=True)\n",
    "\n",
    "chunksize = 1000\n",
    "train_labels, train_predict = [], []\n",
    "\n",
    "for df in tqdm(pd.read_csv(\"mnist-pngs/train_shuffled.csv\", chunksize=chunksize, iterator=True), total=60):\n",
    "    \n",
    "    embedded = embed.transform(df[\"filepath\"])\n",
    "    model.partial_fit(embedded, df[\"label\"], classes=list(range(10)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "999a24ea-be5d-425f-923c-266372c66b5d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "157302965ac8460c97c77935cc08e1fc",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/60 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "train_labels, train_predict = [], []\n",
    "\n",
    "for df in tqdm(pd.read_csv(\"mnist-pngs/train.csv\", chunksize=chunksize, iterator=True), total=60):\n",
    "    df[\"filepath\"] = df[\"filepath\"].apply(lambda x: \"mnist-pngs/\" + x)\n",
    "\n",
    "    embedded = embed.transform(df[\"filepath\"])\n",
    "    train_predict.extend(model.predict(embedded))\n",
    "    train_labels.extend(list(df[\"label\"].values))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c816cd7b-ed3a-4cb2-8aa6-400068a2e414",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7869826407314279a2806bf602a796a8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/10 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "test_labels, test_predict = [], []\n",
    "\n",
    "for df in tqdm(pd.read_csv(\"mnist-pngs/test_shuffled.csv\", chunksize=chunksize, iterator=True), total=10):\n",
    "\n",
    "    embedded = embed.transform(df[\"filepath\"])\n",
    "    test_predict.extend(model.predict(embedded))\n",
    "    test_labels.extend(list(df[\"label\"].values))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4a78add1-7f93-40fc-b119-9dbbe0aa55b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train accuracy: 0.92\n",
      "Test accuracy: 0.92\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "print(f\"Train accuracy: {accuracy_score(train_labels, train_predict):.2f}\")\n",
    "print(f\"Test accuracy: {accuracy_score(test_labels, test_predict):.2f}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
